package com.tiocloud.newpay.feature.bankcard_add;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.ObservableField;

import com.tiocloud.newpay.R;
import com.tiocloud.newpay.databinding.WalletAddBankcardFragmentBinding;
import com.watayouxiang.androidutils.page.MvpFragment;
import com.watayouxiang.androidutils.utils.ClickUtils;
import com.watayouxiang.androidutils.widget.dialog.confirm.EasyConfirmDialog;
import com.watayouxiang.androidutils.widget.dialog.confirm.TioConfirmDialog;
import com.watayouxiang.httpclient.model.response.PayBindCardConfirmResp;

import java.util.Locale;

/**
 * <pre>
 *     author : TaoWang
 *     e-mail : watayouxiang@qq.com
 *     time   : 2021/03/09
 *     desc   : 添加银行卡 fragment
 * </pre>
 */
public class AddBankCardFragment extends MvpFragment<AddBankCardFPresenter, WalletAddBankcardFragmentBinding> implements AddBankCardFContract.View {

    public final ObservableField<String> txt_phone = new ObservableField<>("");
    public final ObservableField<Boolean> isStartTimer = new ObservableField<>(false);
    private boolean isCreditCard = false;

    public static AddBankCardFragment getInstance() {
        AddBankCardFragment fragment = new AddBankCardFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public AddBankCardFPresenter newPresenter() {
        return new AddBankCardFPresenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.wallet_add_bankcard_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding.setData(this);
        presenter.init();
    }

    // 获取验证码
    public void onClick_reqPhoneCode(View view) {
        if (!ClickUtils.isViewSingleClick(view)) return;

        if (isCreditCard) {
            String cardNo = binding.cardNoEtInput.getText().toString().trim();
            String creditCardExpiry = binding.expiryEtInput.getText().toString().trim();
            String creditCardTail = binding.tailEtInput.getText().toString().trim();
            String name = binding.nameEtInput.getText().toString().trim();
            String idNo = binding.idNoEtInput.getText().toString().trim();
            String phone = binding.phoneEtInput.getText().toString().trim();
            presenter.reqSmsCode(cardNo, creditCardExpiry, creditCardTail, name, idNo, phone);
        } else {
            String cardNo = binding.cardNoEtInput.getText().toString().trim();
            String name = binding.nameEtInput.getText().toString().trim();
            String idNo = binding.idNoEtInput.getText().toString().trim();
            String phone = binding.phoneEtInput.getText().toString().trim();
            presenter.reqSmsCode(cardNo, name, idNo, phone);
        }
    }

    // 下一步
    public void onClick_next(View view) {
        if (!ClickUtils.isViewSingleClick(view)) return;
        String smsCode = binding.codeEtInput.getText().toString().trim();
        presenter.reqBindCard(smsCode);
    }

    @Override
    public void resetUI() {
        if (isCreditCard) {
            binding.tvChangeCardType.setText("绑定储蓄卡");
            binding.expiryRlContainer.setVisibility(View.VISIBLE);
            binding.tailRlContainer.setVisibility(View.VISIBLE);
        } else {
            binding.tvChangeCardType.setText("绑定信用卡");
            binding.expiryRlContainer.setVisibility(View.GONE);
            binding.tailRlContainer.setVisibility(View.GONE);
        }

        binding.tvChangeCardType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isCreditCard = !isCreditCard;
                resetUI();
            }
        });
    }

    @Override
    public void onCodeTimerRunning(int second) {
        isStartTimer.set(true);
        binding.phoneTvReqCode.setText(String.format(Locale.getDefault(), "已发送(%ds)", second));
    }

    @Override
    public void onCodeTimerStop() {
        isStartTimer.set(false);
        binding.phoneTvReqCode.setText("获取验证码");
    }

    @Override
    public void showErrorDialog(String msg) {
        if (TextUtils.isEmpty(msg)) {
            msg = "银行卡信息有误，或卡已失效。请核对确认后再试！";
        }
        new EasyConfirmDialog.Builder(getTioActivity())
                .setMessage(msg)
                .setOnConfirmListener(new TioConfirmDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(View view, TioConfirmDialog dialog) {
                        dialog.dismiss();
                    }
                })
                .build()
                .show();
    }

    @Override
    public void reqBindCardSuccess(PayBindCardConfirmResp o) {
        new EasyConfirmDialog.Builder(getTioActivity())
                .setMessage("绑卡成功")
                .setOnConfirmListener(new TioConfirmDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm(View view, TioConfirmDialog dialog) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .build()
                .show();
    }
}
